package henu.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import henu.bean.FeedBack;
import henu.bean.Notice;
import henu.factory.DaoFactory;
import henu.util.ExportExcel;
import henu.util.NowDate;

public class NoticeAction extends ActionSupport {
	HttpServletRequest request = ServletActionContext.getRequest();
	public String add(){
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		String publisher = request.getParameter("publisher");
		String nowTime = NowDate.getNowTime();
		Notice note = new Notice();
		note.setfTitle(title);
		note.setfContent(content);
		note.setfPublisher(publisher);
		note.setfTime(nowTime);
		int n = 0;
		n = DaoFactory.createNoticeDao().add(note);
		if(n == 1){
			return "add";
		}else{
			return "error";
		}
	}
	public String delete(){
		Integer id = Integer.valueOf(request.getParameter("id"));
		int n = 0;
		n = DaoFactory.createNoticeDao().delete(id);
		if(n == 1){
			return "delete";
		}else{
			return "error";
		}
	}
	
	public String updateInfo(){
		Integer id = Integer.valueOf(request.getParameter("id"));
		Notice note = null;
		note = DaoFactory.createNoticeDao().updateInfo(id);
		request.getSession().setAttribute("note", note);
		return "updateInfo";
	}
	
	public String update(){
		int id = Integer.valueOf(request.getParameter("id"));
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		String publisher = request.getParameter("publisher");
		String time = request.getParameter("time");
		Notice note = new Notice();
		note.setId(id);
		note.setfTitle(title);
		note.setfContent(content);
		note.setfPublisher(publisher);
		note.setfTime(time);
		int n = 0;
		n = DaoFactory.createNoticeDao().update(note);
		if(n == 1){
			return "update";
		}else{
			return "error";
		}
	}
	
	public String getAll(){
		String type = request.getParameter("type");
		List<Notice> notes = null;
		notes = DaoFactory.createNoticeDao().getAll();
		request.getSession().setAttribute("notes", notes);
		if(type.equals("tea")){
			return "getAll";
		}else{
			return "stu_getAll";
		}
	}
	
	public String getAllByPage(){
		request = ServletActionContext.getRequest();
		List<Notice> notes = null;
		int pageNow = Integer.parseInt(request.getParameter("num"));
		notes = DaoFactory.createNoticeDao().getAll(5,pageNow);
		int count = (int) DaoFactory.createNoticeDao().getCount();
		System.out.println(count);
		if(count%5==0){
			request.getSession().setAttribute("pageCount", count/5);
		}else{
			request.getSession().setAttribute("pageCount", (count/5)+1);
		}
		request.getSession().setAttribute("notes", notes);
		request.getSession().setAttribute("pageNow", pageNow);
	
		return "stu_getAll";
	}
	
}
